package com.wuge.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;


import com.wuge.gulimall.member.exception.RepeatPhoneException;
import com.wuge.gulimall.member.exception.RepeatUsernameException;
import com.wuge.gulimall.member.vo.UserLoginVo;
import com.wuge.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wuge.gulimall.member.entity.MemberEntity;
import com.wuge.gulimall.member.service.MemberService;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.R;



/**
 * 会员
 *
 * @author wuge
 * @email 1712378864@qq.com
 * @date 2021-07-15 16:30:23
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @PostMapping("/login")
    // @RequiresPermissions("member:member:update")
    public R login(@RequestBody UserLoginVo userLoginVo){
        R ok = R.ok();
        boolean b=memberService.login(userLoginVo,ok);
if (!b){
    return R.error(10005,"账号与密码不匹配");
}
        return ok;
    }
    @GetMapping("/OauthLogin")
    // @RequiresPermissions("member:member:update")
    public R Giteelogin(String code){
        MemberEntity memberEntity=memberService.Authlogin(code);

        return R.ok().put("data",memberEntity);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
  //  @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    @RequestMapping("/register")
    // @RequiresPermissions("member:member:save")
    public R register(@RequestBody UserRegisterVo member){
        try {
            memberService.register(member);
        } catch (RepeatPhoneException e) {
          return R.error(10003,e.getMessage());
        }
        catch (RepeatUsernameException e) {
            return R.error(10004,e.getMessage());
        }

        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
